package cn.iocoder.yudao.module.scrm.strategy.strategyimpl;

import cn.iocoder.yudao.module.scrm.dal.dataobject.business.BusinessDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.clue.ClueDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.schedulescustomer.SchedulesCustomerDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.business.ScrmBusinessMapper;
import cn.iocoder.yudao.module.scrm.dal.mysql.clue.ClueMapper;
import cn.iocoder.yudao.module.scrm.dal.mysql.order.OrderMapper;
import cn.iocoder.yudao.module.scrm.dal.mysql.schedulescustomer.SchedulesCustomerMapper;
import cn.iocoder.yudao.module.scrm.strategy.DeleteStrategy;
import groovy.lang.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.stream.Stream;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.CUSTOMER_EXIST_RELEVANT_DATA;

/**
 * @Author LiZhiMin
 * @Date 2024/9/13 9:36
 */
@Component
public class CustomerDeleteStrategyImpl implements DeleteStrategy {

    @Resource
    @Lazy//延迟加载bean
    private ClueMapper clueMapper;

    @Resource
    @Lazy//延迟加载bean
    private ScrmBusinessMapper businessMapper;

    @Resource
    @Lazy//延迟加载bean
    private SchedulesCustomerMapper schedulesCustomerMapper;

    @Resource
    @Lazy//延迟加载bean
    private OrderMapper orderMapper;


    @Override
    public void deleteCheck(Long customerId) {
        existRelevantData(customerId);
    }


    private void existRelevantData(Long customerId) {
        // 定义一个布尔值来表示是否存在相关数据
        if (Stream.of(
                clueMapper.selectCount(ClueDO::getCustomerId, customerId),
                businessMapper.selectCount(BusinessDO::getCustomerId, customerId),
                schedulesCustomerMapper.selectCount(SchedulesCustomerDO::getCustomerId, customerId),
                orderMapper.selectCount(OrderDO::getCustomerId, customerId)).mapToLong(Long::longValue).sum() > 0) {
            throw exception(CUSTOMER_EXIST_RELEVANT_DATA);
        }
    }
}
